package com.groupfifteen.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.groupfifteen.dao.SysPaperManageDao;
import com.groupfifteen.entity.SysFillQuestionEntity;
import com.groupfifteen.entity.SysJudgeQuestionEntity;
import com.groupfifteen.entity.SysMultiQuestionEntity;
import com.groupfifteen.entity.SysPaperManageEntity;
import com.groupfifteen.service.SysPaperManageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("sysPaperManageService")
@RequiredArgsConstructor
public class SysPaperManageServiceImpl extends ServiceImpl<SysPaperManageDao, SysPaperManageEntity>
        implements SysPaperManageService {
    private final SysPaperManageDao paperMapper;

    private final SysJudgeQuestionServiceImpl judgeQuestionService;

    private final SysMultiQuestionServiceImpl multiQuestionService;

    private final SysFillQuestionServiceImpl fillQuestionService;
    @Override
    public IPage<SysMultiQuestionEntity> QueryPaperManages(SysPaperManageEntity sysPaperManageEntity, int pageNo, int pageSize) {
        return null;
    }

    @Override
    public IPage<SysPaperManageEntity> QuerySysPaperManages(SysPaperManageEntity sysPaperManageEntity, int pageNo, int pageSize) {
        LambdaQueryWrapper<SysPaperManageEntity> query = Wrappers.lambdaQuery();
        // 假设我们根据paperId进行查询，您可以根据实际需求添加更多的查询条件
        query.eq(sysPaperManageEntity.getPaperId() != null, SysPaperManageEntity::getPaperId, sysPaperManageEntity.getPaperId());

        // 确保页码和页大小有效
        if (pageNo <= 0) {
            pageNo = 1;
        }
        if (pageSize <= 0) {
            pageSize = 10;
        }

        // 创建分页对象
        IPage<SysPaperManageEntity> page = new Page<>(pageNo, pageSize);
        // 执行分页查询
        return baseMapper.selectPage(page, query);
    }

    // 在这里实现 SysPaperManageService 接口中的其他方法

    @Override
    public Integer AddPaper(SysPaperManageEntity sysPaperManageEntity) {
        return baseMapper.insert(sysPaperManageEntity);
    }

    @Override
    public Integer DeletePaper(Integer paper_id) {
        return baseMapper.deleteById(paper_id);
    }

    @Override
    public SysPaperManageEntity GetPaper(Integer paper_id) {
        return baseMapper.selectById(paper_id);
    }

    @Override
    public Integer UpdatePaper(SysPaperManageEntity sysPaperManageEntity) {
        return baseMapper.updateById(sysPaperManageEntity);
    }
    /**
     * 查询所有试卷
     *
     * @return 试卷
     */
    @Override
    public List<SysPaperManageEntity> findAll() {
        return paperMapper.findAll();
    }

    /**
     * 添加试卷
     *
     * @param paperManage 试卷信息
     * @return 结果
     */
    @Override
    public Integer add(SysPaperManageEntity paperManage) {
        return paperMapper.add(paperManage);
    }

    /**
     * 获取试卷总分
     *
     * @param paperId 试卷id
     * @return 分数
     */
    @Override
    public Integer getMaxScore(Integer paperId) {

        List<SysMultiQuestionEntity> multiQuestionRes = multiQuestionService.findByIdAndType(paperId);   //选择题题库 1
        List<SysFillQuestionEntity> fillQuestionsRes = fillQuestionService.findByIdAndType(paperId);     //填空题题库 2
        List<SysJudgeQuestionEntity> judgeQuestionRes = judgeQuestionService.findByIdAndType(paperId);   //判断题题库 3
        return 2 * (multiQuestionRes.size() + fillQuestionsRes.size() + judgeQuestionRes.size());
    }

    /**
     * 删除试卷中的某条试题
     *
     * @param paperId    试卷id
     * @param type       题目类型。1选择，2填空，3判断
     * @param questionId 题目id
     */
    @Override
    public void delete(String paperId, String type, String questionId) {
        paperMapper.delete(paperId, type, questionId);
    }
}